/****************************************************************************
 *
 * Copyright 2018 Samsung Electronics All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 ****************************************************************************/
/****************************************************************************
 * examples/grpc/greeter_client/greeter_client.cc
 *
 *   Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#include <tinyara/config.h>
#include <iostream>
#include <memory>
#include <string>

#include <grpc++/grpc++.h>
#include <tinyara/init.h>

#include <stdlib.h>

#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using grpc::ChannelCredentials;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;

class GreeterClient
{
public:
	GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel))
	{
	}

	// Assembles the client's payload, sends it and presents the response back
	// from the server.
	std::string SayHello(const std::string& user)
	{
		// Data we are sending to the server.
		HelloRequest request;
		request.set_name(user);

		// Container for the data we expect from the server.
		HelloReply reply;

		// Context for the client. It could be used to convey extra information to
		// the server and/or tweak certain RPC behaviors.
		ClientContext context;

		// The actual RPC.
		Status status = stub_->SayHello(&context, request, &reply);

		// Act upon its status.
		if (status.ok()) {
			return reply.message();
		} else {
			std::cout << status.error_code() << ": " << status.error_message() << std::endl;
			return "RPC failed";
		}
	}
	/*
	std::string SayHelloAgain(const std::string& user) {
	  // Follows the same pattern as SayHello.
	  HelloRequest request;
	  request.set_name(user);
	  HelloReply reply;
	  ClientContext context;

	  // Here we can the stub's newly available method we just added.
	  Status status = stub_->SayHelloAgain(&context, request, &reply);
	  if (status.ok()) {
		return reply.message();
	  } else {
		std::cout << status.error_code() << ": " << status.error_message()
				  << std::endl;
		return "RPC failed";
	  }
	}
	*/

private:
	std::unique_ptr<Greeter::Stub> stub_;
};

/* test_rootca_private_key. This will use on server-side to enable secure channel */
/*
const char test_server_key[] = {
  0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52,
  0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54, 0x45, 0x20, 0x4b,
  0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x45,
  0x70, 0x41, 0x49, 0x42, 0x41, 0x41, 0x4b, 0x43, 0x41, 0x51, 0x45, 0x41,
  0x74, 0x70, 0x4b, 0x6f, 0x58, 0x35, 0x41, 0x73, 0x6d, 0x35, 0x38, 0x34,
  0x57, 0x35, 0x6e, 0x44, 0x2f, 0x4c, 0x79, 0x64, 0x58, 0x30, 0x64, 0x63,
  0x38, 0x39, 0x72, 0x43, 0x41, 0x34, 0x75, 0x35, 0x4e, 0x43, 0x36, 0x32,
  0x77, 0x4e, 0x79, 0x47, 0x30, 0x77, 0x7a, 0x77, 0x2b, 0x52, 0x52, 0x72,
  0x0a, 0x43, 0x72, 0x41, 0x50, 0x36, 0x62, 0x65, 0x68, 0x43, 0x76, 0x41,
  0x2f, 0x49, 0x71, 0x32, 0x37, 0x39, 0x68, 0x6f, 0x4b, 0x4e, 0x59, 0x61,
  0x69, 0x72, 0x38, 0x78, 0x31, 0x2b, 0x35, 0x35, 0x36, 0x75, 0x48, 0x56,
  0x58, 0x33, 0x35, 0x61, 0x6e, 0x51, 0x58, 0x43, 0x6d, 0x48, 0x4e, 0x37,
  0x63, 0x42, 0x39, 0x32, 0x54, 0x74, 0x6c, 0x30, 0x34, 0x69, 0x7a, 0x56,
  0x72, 0x57, 0x57, 0x4d, 0x6a, 0x0a, 0x68, 0x43, 0x58, 0x6a, 0x57, 0x4f,
  0x56, 0x70, 0x4e, 0x54, 0x6b, 0x2b, 0x6c, 0x31, 0x67, 0x78, 0x39, 0x53,
  0x71, 0x58, 0x59, 0x57, 0x59, 0x58, 0x44, 0x54, 0x4d, 0x49, 0x30, 0x4a,
  0x49, 0x52, 0x41, 0x73, 0x62, 0x39, 0x61, 0x45, 0x78, 0x71, 0x2b, 0x4d,
  0x78, 0x61, 0x55, 0x6e, 0x4e, 0x65, 0x30, 0x52, 0x6e, 0x4e, 0x52, 0x79,
  0x71, 0x43, 0x70, 0x50, 0x43, 0x76, 0x32, 0x5a, 0x36, 0x5a, 0x0a, 0x78,
  0x50, 0x47, 0x7a, 0x43, 0x7a, 0x31, 0x61, 0x79, 0x49, 0x56, 0x44, 0x6d,
  0x61, 0x45, 0x4b, 0x48, 0x6a, 0x31, 0x59, 0x55, 0x47, 0x36, 0x55, 0x53,
  0x62, 0x32, 0x45, 0x79, 0x45, 0x64, 0x33, 0x6d, 0x7a, 0x4a, 0x6c, 0x77,
  0x38, 0x4f, 0x6b, 0x6f, 0x42, 0x50, 0x43, 0x49, 0x62, 0x4a, 0x49, 0x56,
  0x30, 0x55, 0x73, 0x6d, 0x59, 0x41, 0x2b, 0x64, 0x6b, 0x48, 0x6e, 0x65,
  0x71, 0x36, 0x63, 0x0a, 0x6a, 0x68, 0x52, 0x51, 0x53, 0x2f, 0x31, 0x50,
  0x38, 0x52, 0x30, 0x57, 0x58, 0x65, 0x77, 0x2b, 0x2f, 0x65, 0x78, 0x66,
  0x51, 0x64, 0x37, 0x74, 0x64, 0x37, 0x50, 0x39, 0x31, 0x51, 0x6f, 0x62,
  0x39, 0x34, 0x4a, 0x2f, 0x41, 0x43, 0x63, 0x32, 0x6b, 0x32, 0x45, 0x6e,
  0x78, 0x67, 0x61, 0x42, 0x44, 0x2f, 0x41, 0x30, 0x72, 0x4a, 0x35, 0x41,
  0x72, 0x73, 0x6d, 0x41, 0x48, 0x70, 0x33, 0x61, 0x0a, 0x52, 0x38, 0x4b,
  0x72, 0x69, 0x61, 0x33, 0x52, 0x44, 0x79, 0x69, 0x59, 0x7a, 0x66, 0x59,
  0x55, 0x48, 0x36, 0x52, 0x45, 0x78, 0x48, 0x68, 0x37, 0x32, 0x44, 0x45,
  0x79, 0x5a, 0x4a, 0x56, 0x6a, 0x45, 0x5a, 0x58, 0x58, 0x55, 0x77, 0x49,
  0x44, 0x41, 0x51, 0x41, 0x42, 0x41, 0x6f, 0x49, 0x42, 0x41, 0x43, 0x53,
  0x71, 0x71, 0x35, 0x6f, 0x56, 0x73, 0x48, 0x2f, 0x49, 0x71, 0x46, 0x37,
  0x74, 0x0a, 0x4d, 0x4d, 0x6c, 0x50, 0x63, 0x43, 0x4f, 0x30, 0x68, 0x31,
  0x79, 0x4b, 0x4d, 0x4c, 0x44, 0x5a, 0x6e, 0x4f, 0x5a, 0x34, 0x41, 0x30,
  0x67, 0x55, 0x67, 0x68, 0x64, 0x63, 0x34, 0x41, 0x43, 0x47, 0x4d, 0x30,
  0x46, 0x41, 0x50, 0x74, 0x48, 0x6e, 0x69, 0x54, 0x5a, 0x54, 0x4c, 0x62,
  0x72, 0x66, 0x50, 0x59, 0x4b, 0x64, 0x6c, 0x44, 0x4e, 0x50, 0x79, 0x4e,
  0x69, 0x5a, 0x72, 0x5a, 0x42, 0x4e, 0x0a, 0x4d, 0x63, 0x52, 0x64, 0x2f,
  0x4c, 0x6e, 0x77, 0x33, 0x63, 0x4a, 0x73, 0x78, 0x30, 0x2f, 0x42, 0x43,
  0x6c, 0x75, 0x69, 0x43, 0x6f, 0x70, 0x7a, 0x69, 0x6d, 0x6d, 0x61, 0x33,
  0x75, 0x5a, 0x69, 0x33, 0x4c, 0x30, 0x72, 0x68, 0x73, 0x39, 0x52, 0x73,
  0x36, 0x46, 0x32, 0x7a, 0x37, 0x56, 0x42, 0x68, 0x7a, 0x62, 0x2f, 0x79,
  0x4d, 0x74, 0x4d, 0x33, 0x35, 0x42, 0x4e, 0x45, 0x5a, 0x59, 0x68, 0x0a,
  0x33, 0x37, 0x79, 0x35, 0x44, 0x69, 0x65, 0x6d, 0x51, 0x55, 0x32, 0x61,
  0x62, 0x51, 0x46, 0x6d, 0x48, 0x6b, 0x4c, 0x4d, 0x64, 0x63, 0x65, 0x33,
  0x4a, 0x2b, 0x6e, 0x4c, 0x2f, 0x64, 0x79, 0x6b, 0x4c, 0x36, 0x45, 0x66,
  0x64, 0x77, 0x6b, 0x66, 0x58, 0x30, 0x66, 0x64, 0x79, 0x4a, 0x68, 0x31,
  0x55, 0x4d, 0x46, 0x70, 0x32, 0x2b, 0x61, 0x67, 0x6c, 0x6b, 0x65, 0x2b,
  0x5a, 0x37, 0x31, 0x4a, 0x0a, 0x54, 0x74, 0x69, 0x35, 0x76, 0x4c, 0x56,
  0x65, 0x33, 0x54, 0x32, 0x50, 0x30, 0x63, 0x4c, 0x39, 0x78, 0x75, 0x57,
  0x45, 0x45, 0x49, 0x6f, 0x79, 0x72, 0x68, 0x5a, 0x4c, 0x72, 0x6a, 0x4f,
  0x56, 0x6b, 0x37, 0x65, 0x66, 0x68, 0x4f, 0x50, 0x51, 0x35, 0x41, 0x30,
  0x74, 0x6a, 0x50, 0x38, 0x42, 0x6b, 0x74, 0x31, 0x51, 0x65, 0x57, 0x55,
  0x62, 0x4e, 0x58, 0x2b, 0x61, 0x47, 0x64, 0x61, 0x72, 0x0a, 0x46, 0x44,
  0x76, 0x64, 0x50, 0x32, 0x38, 0x46, 0x38, 0x4b, 0x72, 0x78, 0x32, 0x67,
  0x71, 0x48, 0x4d, 0x2f, 0x63, 0x4d, 0x69, 0x50, 0x4a, 0x74, 0x62, 0x4b,
  0x64, 0x4f, 0x32, 0x4f, 0x4d, 0x62, 0x39, 0x63, 0x61, 0x6e, 0x6a, 0x71,
  0x2f, 0x6e, 0x31, 0x77, 0x43, 0x66, 0x68, 0x2b, 0x35, 0x58, 0x54, 0x56,
  0x45, 0x72, 0x49, 0x79, 0x44, 0x2b, 0x54, 0x45, 0x42, 0x43, 0x58, 0x63,
  0x36, 0x56, 0x0a, 0x42, 0x62, 0x6e, 0x64, 0x77, 0x76, 0x45, 0x43, 0x67,
  0x59, 0x45, 0x41, 0x35, 0x79, 0x38, 0x78, 0x71, 0x4d, 0x43, 0x71, 0x77,
  0x35, 0x53, 0x42, 0x64, 0x6f, 0x70, 0x70, 0x4b, 0x31, 0x4d, 0x41, 0x52,
  0x6a, 0x76, 0x61, 0x6e, 0x47, 0x65, 0x50, 0x61, 0x64, 0x77, 0x54, 0x4b,
  0x49, 0x77, 0x71, 0x33, 0x6f, 0x59, 0x43, 0x5a, 0x76, 0x4b, 0x67, 0x76,
  0x5a, 0x78, 0x59, 0x2f, 0x31, 0x35, 0x78, 0x0a, 0x70, 0x6c, 0x39, 0x42,
  0x51, 0x41, 0x4e, 0x35, 0x62, 0x37, 0x4c, 0x5a, 0x4d, 0x4a, 0x74, 0x36,
  0x6f, 0x4b, 0x37, 0x53, 0x71, 0x45, 0x57, 0x44, 0x2b, 0x39, 0x38, 0x70,
  0x39, 0x33, 0x38, 0x48, 0x76, 0x31, 0x64, 0x65, 0x65, 0x41, 0x42, 0x35,
  0x59, 0x79, 0x6e, 0x45, 0x6d, 0x57, 0x5a, 0x74, 0x43, 0x66, 0x79, 0x6a,
  0x75, 0x71, 0x46, 0x7a, 0x61, 0x6a, 0x4f, 0x53, 0x2f, 0x49, 0x79, 0x61,
  0x0a, 0x6e, 0x45, 0x45, 0x4f, 0x43, 0x30, 0x52, 0x35, 0x6c, 0x65, 0x6f,
  0x31, 0x7a, 0x43, 0x47, 0x5a, 0x6e, 0x46, 0x4f, 0x46, 0x4b, 0x33, 0x56,
  0x34, 0x76, 0x4a, 0x31, 0x57, 0x31, 0x6c, 0x45, 0x39, 0x36, 0x79, 0x33,
  0x76, 0x45, 0x67, 0x52, 0x41, 0x53, 0x71, 0x64, 0x6a, 0x65, 0x57, 0x56,
  0x4a, 0x51, 0x46, 0x55, 0x4d, 0x57, 0x64, 0x63, 0x43, 0x67, 0x59, 0x45,
  0x41, 0x79, 0x69, 0x75, 0x6d, 0x0a, 0x5a, 0x43, 0x78, 0x36, 0x62, 0x34,
  0x79, 0x30, 0x56, 0x6b, 0x2f, 0x54, 0x7a, 0x31, 0x56, 0x39, 0x54, 0x63,
  0x55, 0x6f, 0x38, 0x54, 0x46, 0x70, 0x46, 0x6b, 0x57, 0x56, 0x31, 0x4d,
  0x71, 0x65, 0x31, 0x51, 0x62, 0x63, 0x61, 0x52, 0x76, 0x56, 0x46, 0x7a,
  0x33, 0x37, 0x48, 0x55, 0x52, 0x55, 0x79, 0x50, 0x58, 0x52, 0x65, 0x45,
  0x38, 0x71, 0x6a, 0x52, 0x54, 0x55, 0x75, 0x73, 0x61, 0x7a, 0x0a, 0x55,
  0x52, 0x51, 0x4f, 0x39, 0x62, 0x69, 0x6a, 0x75, 0x31, 0x6b, 0x51, 0x2f,
  0x67, 0x64, 0x43, 0x63, 0x62, 0x37, 0x2f, 0x44, 0x4a, 0x75, 0x45, 0x45,
  0x4a, 0x77, 0x68, 0x73, 0x32, 0x4a, 0x44, 0x73, 0x78, 0x2b, 0x74, 0x39,
  0x68, 0x34, 0x61, 0x45, 0x6e, 0x52, 0x66, 0x6a, 0x71, 0x38, 0x52, 0x77,
  0x4e, 0x49, 0x6b, 0x4c, 0x4d, 0x57, 0x36, 0x70, 0x4e, 0x64, 0x43, 0x4e,
  0x44, 0x33, 0x32, 0x0a, 0x66, 0x6a, 0x35, 0x62, 0x67, 0x55, 0x47, 0x68,
  0x30, 0x34, 0x4d, 0x41, 0x73, 0x33, 0x6c, 0x74, 0x68, 0x49, 0x74, 0x51,
  0x68, 0x51, 0x57, 0x43, 0x35, 0x51, 0x4f, 0x4e, 0x45, 0x64, 0x31, 0x77,
  0x4f, 0x70, 0x43, 0x58, 0x46, 0x75, 0x55, 0x43, 0x67, 0x59, 0x45, 0x41,
  0x72, 0x42, 0x39, 0x74, 0x48, 0x37, 0x46, 0x69, 0x38, 0x6e, 0x37, 0x73,
  0x37, 0x4f, 0x38, 0x63, 0x59, 0x6b, 0x39, 0x48, 0x0a, 0x30, 0x77, 0x4c,
  0x5a, 0x50, 0x72, 0x32, 0x7a, 0x2f, 0x79, 0x4d, 0x46, 0x65, 0x4a, 0x6a,
  0x7a, 0x69, 0x66, 0x33, 0x49, 0x61, 0x42, 0x47, 0x5a, 0x4c, 0x39, 0x4c,
  0x71, 0x53, 0x43, 0x64, 0x76, 0x41, 0x6d, 0x6e, 0x46, 0x55, 0x50, 0x33,
  0x46, 0x4f, 0x69, 0x36, 0x30, 0x6d, 0x67, 0x59, 0x49, 0x68, 0x50, 0x33,
  0x6b, 0x68, 0x62, 0x68, 0x4d, 0x44, 0x67, 0x6b, 0x58, 0x57, 0x39, 0x66,
  0x4f, 0x0a, 0x55, 0x74, 0x32, 0x66, 0x7a, 0x6b, 0x53, 0x38, 0x47, 0x2b,
  0x4b, 0x4c, 0x4a, 0x35, 0x4d, 0x6b, 0x39, 0x39, 0x79, 0x4c, 0x6f, 0x6e,
  0x5a, 0x32, 0x55, 0x63, 0x6e, 0x59, 0x56, 0x41, 0x50, 0x7a, 0x55, 0x51,
  0x62, 0x32, 0x68, 0x39, 0x6b, 0x52, 0x6c, 0x56, 0x5a, 0x74, 0x65, 0x33,
  0x42, 0x41, 0x79, 0x43, 0x30, 0x2b, 0x35, 0x78, 0x35, 0x74, 0x4b, 0x44,
  0x45, 0x55, 0x36, 0x44, 0x68, 0x51, 0x0a, 0x6d, 0x33, 0x72, 0x31, 0x6c,
  0x6a, 0x70, 0x6f, 0x43, 0x36, 0x30, 0x59, 0x32, 0x4e, 0x64, 0x61, 0x64,
  0x48, 0x56, 0x64, 0x33, 0x72, 0x30, 0x43, 0x67, 0x59, 0x42, 0x39, 0x70,
  0x46, 0x7a, 0x33, 0x47, 0x6e, 0x46, 0x49, 0x79, 0x2f, 0x2f, 0x7a, 0x4f,
  0x41, 0x38, 0x68, 0x5a, 0x6a, 0x62, 0x47, 0x55, 0x30, 0x5a, 0x62, 0x68,
  0x75, 0x4b, 0x52, 0x4e, 0x64, 0x4f, 0x6b, 0x39, 0x2f, 0x74, 0x33, 0x0a,
  0x31, 0x70, 0x69, 0x36, 0x4e, 0x64, 0x4c, 0x62, 0x61, 0x2b, 0x69, 0x59,
  0x79, 0x72, 0x4f, 0x4b, 0x5a, 0x4d, 0x4b, 0x32, 0x61, 0x7a, 0x53, 0x39,
  0x6b, 0x76, 0x6d, 0x56, 0x7a, 0x68, 0x44, 0x34, 0x7a, 0x57, 0x64, 0x79,
  0x72, 0x44, 0x6f, 0x4e, 0x48, 0x35, 0x41, 0x67, 0x33, 0x7a, 0x41, 0x61,
  0x38, 0x71, 0x45, 0x64, 0x47, 0x37, 0x55, 0x61, 0x67, 0x58, 0x32, 0x73,
  0x4e, 0x75, 0x42, 0x74, 0x0a, 0x2f, 0x4c, 0x30, 0x6e, 0x48, 0x4a, 0x51,
  0x46, 0x62, 0x67, 0x39, 0x49, 0x35, 0x47, 0x49, 0x37, 0x48, 0x42, 0x33,
  0x68, 0x4f, 0x55, 0x30, 0x43, 0x54, 0x73, 0x6a, 0x56, 0x49, 0x4d, 0x45,
  0x45, 0x75, 0x67, 0x6f, 0x79, 0x4c, 0x54, 0x43, 0x39, 0x2b, 0x30, 0x63,
  0x2f, 0x51, 0x72, 0x4b, 0x65, 0x55, 0x64, 0x70, 0x4c, 0x6f, 0x52, 0x34,
  0x30, 0x35, 0x31, 0x30, 0x50, 0x67, 0x6a, 0x66, 0x53, 0x0a, 0x48, 0x62,
  0x50, 0x59, 0x52, 0x51, 0x4b, 0x42, 0x67, 0x51, 0x43, 0x50, 0x72, 0x61,
  0x53, 0x64, 0x30, 0x5a, 0x59, 0x53, 0x4e, 0x46, 0x45, 0x77, 0x32, 0x4b,
  0x59, 0x69, 0x4d, 0x78, 0x76, 0x73, 0x64, 0x4e, 0x70, 0x5a, 0x78, 0x4f,
  0x69, 0x42, 0x6b, 0x77, 0x74, 0x37, 0x66, 0x44, 0x33, 0x66, 0x67, 0x52,
  0x67, 0x36, 0x46, 0x78, 0x31, 0x64, 0x59, 0x50, 0x59, 0x56, 0x54, 0x46,
  0x47, 0x30, 0x0a, 0x43, 0x30, 0x75, 0x6f, 0x61, 0x73, 0x2b, 0x4b, 0x49,
  0x4e, 0x54, 0x35, 0x64, 0x45, 0x6d, 0x43, 0x52, 0x69, 0x45, 0x48, 0x30,
  0x55, 0x6b, 0x4f, 0x51, 0x64, 0x4f, 0x41, 0x44, 0x77, 0x41, 0x4b, 0x56,
  0x43, 0x63, 0x4a, 0x5a, 0x2f, 0x31, 0x35, 0x4a, 0x44, 0x51, 0x58, 0x71,
  0x5a, 0x63, 0x36, 0x53, 0x30, 0x78, 0x70, 0x42, 0x50, 0x32, 0x61, 0x30,
  0x42, 0x71, 0x4a, 0x6d, 0x75, 0x6e, 0x58, 0x0a, 0x48, 0x43, 0x36, 0x49,
  0x38, 0x6b, 0x58, 0x4e, 0x54, 0x56, 0x44, 0x32, 0x65, 0x78, 0x76, 0x78,
  0x75, 0x54, 0x73, 0x57, 0x45, 0x4c, 0x6f, 0x57, 0x34, 0x56, 0x32, 0x56,
  0x34, 0x39, 0x72, 0x33, 0x78, 0x72, 0x70, 0x71, 0x44, 0x41, 0x57, 0x48,
  0x30, 0x4e, 0x47, 0x38, 0x41, 0x45, 0x6a, 0x6d, 0x74, 0x2b, 0x41, 0x42,
  0x6f, 0x67, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e,
  0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x52, 0x49, 0x56, 0x41, 0x54,
  0x45, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x00
};

<How To Enable Secure Channel on ServerSide>

using grpc::ServerCredentials;
using grpc::SslServerCredentials;
using grpc::SslServerCredentialsOptions;

std::shared_ptr<ServerCredentials> creds;
grpc::SslServerCredentialsOptions ssl_opts;
// To verify client cert on server-side, you should add the client's cert here.
ssl_opts.pem_root_certs="";
// Add server's cert & private key
grpc::SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server_key, test_root_cert};
ssl_opts.pem_key_cert_pairs.push_back(pkcp);
creds = grpc::SslServerCredentials(ssl_opts);

builder.AddListeningPort(server_address, creds);
*/

const char test_root_cert[] = {
	0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43,
	0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x49, 0x44, 0x43, 0x43, 0x41, 0x67,
	0x67, 0x43, 0x41, 0x51, 0x45, 0x77, 0x44, 0x51, 0x59, 0x4a, 0x4b, 0x6f, 0x5a, 0x49, 0x68, 0x76, 0x63, 0x4e, 0x41,
	0x51, 0x45, 0x4c, 0x42, 0x51, 0x41, 0x77, 0x56, 0x6a, 0x45, 0x4c, 0x4d, 0x41, 0x6b, 0x47, 0x41, 0x31, 0x55, 0x45,
	0x42, 0x68, 0x4d, 0x43, 0x51, 0x56, 0x55, 0x78, 0x45, 0x7a, 0x41, 0x52, 0x42, 0x67, 0x4e, 0x56, 0x0a, 0x42, 0x41,
	0x67, 0x4d, 0x43, 0x6c, 0x4e, 0x76, 0x62, 0x57, 0x55, 0x74, 0x55, 0x33, 0x52, 0x68, 0x64, 0x47, 0x55, 0x78, 0x49,
	0x54, 0x41, 0x66, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x6f, 0x4d, 0x47, 0x45, 0x6c, 0x75, 0x64, 0x47, 0x56, 0x79,
	0x62, 0x6d, 0x56, 0x30, 0x49, 0x46, 0x64, 0x70, 0x5a, 0x47, 0x64, 0x70, 0x64, 0x48, 0x4d, 0x67, 0x55, 0x48, 0x52,
	0x35, 0x49, 0x45, 0x78, 0x30, 0x0a, 0x5a, 0x44, 0x45, 0x50, 0x4d, 0x41, 0x30, 0x47, 0x41, 0x31, 0x55, 0x45, 0x41,
	0x77, 0x77, 0x47, 0x64, 0x47, 0x56, 0x7a, 0x64, 0x47, 0x4e, 0x68, 0x4d, 0x42, 0x34, 0x58, 0x44, 0x54, 0x45, 0x34,
	0x4d, 0x44, 0x45, 0x7a, 0x4d, 0x54, 0x41, 0x35, 0x4d, 0x54, 0x63, 0x31, 0x4d, 0x56, 0x6f, 0x58, 0x44, 0x54, 0x49,
	0x34, 0x4d, 0x44, 0x45, 0x79, 0x4f, 0x54, 0x41, 0x35, 0x4d, 0x54, 0x63, 0x31, 0x0a, 0x4d, 0x56, 0x6f, 0x77, 0x56,
	0x6a, 0x45, 0x4c, 0x4d, 0x41, 0x6b, 0x47, 0x41, 0x31, 0x55, 0x45, 0x42, 0x68, 0x4d, 0x43, 0x51, 0x56, 0x55, 0x78,
	0x45, 0x7a, 0x41, 0x52, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41, 0x67, 0x4d, 0x43, 0x6c, 0x4e, 0x76, 0x62, 0x57, 0x55,
	0x74, 0x55, 0x33, 0x52, 0x68, 0x64, 0x47, 0x55, 0x78, 0x49, 0x54, 0x41, 0x66, 0x42, 0x67, 0x4e, 0x56, 0x42, 0x41,
	0x6f, 0x4d, 0x0a, 0x47, 0x45, 0x6c, 0x75, 0x64, 0x47, 0x56, 0x79, 0x62, 0x6d, 0x56, 0x30, 0x49, 0x46, 0x64, 0x70,
	0x5a, 0x47, 0x64, 0x70, 0x64, 0x48, 0x4d, 0x67, 0x55, 0x48, 0x52, 0x35, 0x49, 0x45, 0x78, 0x30, 0x5a, 0x44, 0x45,
	0x50, 0x4d, 0x41, 0x30, 0x47, 0x41, 0x31, 0x55, 0x45, 0x41, 0x77, 0x77, 0x47, 0x64, 0x47, 0x56, 0x7a, 0x64, 0x47,
	0x4e, 0x68, 0x4d, 0x49, 0x49, 0x42, 0x49, 0x6a, 0x41, 0x4e, 0x0a, 0x42, 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47,
	0x39, 0x77, 0x30, 0x42, 0x41, 0x51, 0x45, 0x46, 0x41, 0x41, 0x4f, 0x43, 0x41, 0x51, 0x38, 0x41, 0x4d, 0x49, 0x49,
	0x42, 0x43, 0x67, 0x4b, 0x43, 0x41, 0x51, 0x45, 0x41, 0x32, 0x59, 0x33, 0x4a, 0x53, 0x31, 0x41, 0x42, 0x43, 0x65,
	0x4b, 0x46, 0x71, 0x79, 0x78, 0x6c, 0x48, 0x51, 0x6c, 0x44, 0x55, 0x75, 0x35, 0x74, 0x67, 0x63, 0x33, 0x2f, 0x0a,
	0x38, 0x77, 0x61, 0x75, 0x66, 0x7a, 0x4f, 0x77, 0x75, 0x54, 0x41, 0x6c, 0x79, 0x35, 0x61, 0x70, 0x75, 0x37, 0x4b,
	0x57, 0x59, 0x49, 0x6a, 0x59, 0x41, 0x64, 0x58, 0x5a, 0x6e, 0x54, 0x4b, 0x73, 0x52, 0x6e, 0x2f, 0x7a, 0x43, 0x42,
	0x4c, 0x59, 0x73, 0x76, 0x43, 0x78, 0x78, 0x67, 0x77, 0x50, 0x49, 0x42, 0x61, 0x63, 0x46, 0x31, 0x42, 0x44, 0x34,
	0x6e, 0x70, 0x41, 0x33, 0x63, 0x54, 0x41, 0x0a, 0x72, 0x70, 0x6c, 0x5a, 0x78, 0x69, 0x38, 0x4d, 0x57, 0x4d, 0x6f,
	0x79, 0x73, 0x2b, 0x32, 0x51, 0x49, 0x4a, 0x46, 0x6e, 0x54, 0x53, 0x31, 0x5a, 0x2b, 0x47, 0x62, 0x42, 0x57, 0x6f,
	0x71, 0x65, 0x6e, 0x38, 0x5a, 0x61, 0x36, 0x51, 0x71, 0x4a, 0x63, 0x73, 0x5a, 0x42, 0x71, 0x74, 0x79, 0x6b, 0x6b,
	0x44, 0x38, 0x42, 0x47, 0x33, 0x2f, 0x6c, 0x6f, 0x45, 0x4b, 0x43, 0x58, 0x76, 0x44, 0x45, 0x0a, 0x42, 0x4b, 0x36,
	0x2f, 0x33, 0x71, 0x38, 0x52, 0x56, 0x7a, 0x77, 0x33, 0x58, 0x42, 0x46, 0x43, 0x67, 0x4c, 0x68, 0x6d, 0x47, 0x59,
	0x57, 0x4c, 0x66, 0x42, 0x6f, 0x2b, 0x4b, 0x36, 0x6e, 0x52, 0x79, 0x38, 0x59, 0x5a, 0x34, 0x59, 0x34, 0x6d, 0x79,
	0x52, 0x73, 0x55, 0x38, 0x77, 0x59, 0x38, 0x6f, 0x54, 0x55, 0x77, 0x31, 0x39, 0x4c, 0x50, 0x4e, 0x79, 0x36, 0x50,
	0x2f, 0x46, 0x44, 0x74, 0x0a, 0x4c, 0x68, 0x57, 0x54, 0x61, 0x2b, 0x43, 0x57, 0x36, 0x4e, 0x33, 0x6f, 0x58, 0x6f,
	0x73, 0x64, 0x32, 0x76, 0x38, 0x79, 0x30, 0x72, 0x72, 0x63, 0x64, 0x58, 0x74, 0x52, 0x63, 0x61, 0x30, 0x58, 0x64,
	0x57, 0x50, 0x4c, 0x50, 0x72, 0x6a, 0x79, 0x44, 0x76, 0x52, 0x6a, 0x53, 0x39, 0x69, 0x57, 0x4f, 0x4c, 0x70, 0x64,
	0x63, 0x79, 0x53, 0x41, 0x50, 0x52, 0x51, 0x6f, 0x53, 0x33, 0x48, 0x32, 0x0a, 0x4d, 0x72, 0x30, 0x4b, 0x4d, 0x32,
	0x67, 0x76, 0x48, 0x71, 0x33, 0x36, 0x45, 0x55, 0x50, 0x71, 0x67, 0x43, 0x45, 0x7a, 0x68, 0x64, 0x74, 0x71, 0x30,
	0x4d, 0x33, 0x68, 0x5a, 0x7a, 0x4e, 0x30, 0x37, 0x4c, 0x49, 0x46, 0x4c, 0x73, 0x43, 0x6a, 0x52, 0x57, 0x4d, 0x6c,
	0x35, 0x59, 0x56, 0x68, 0x6f, 0x74, 0x54, 0x69, 0x65, 0x49, 0x50, 0x7a, 0x46, 0x51, 0x49, 0x44, 0x41, 0x51, 0x41,
	0x42, 0x0a, 0x4d, 0x41, 0x30, 0x47, 0x43, 0x53, 0x71, 0x47, 0x53, 0x49, 0x62, 0x33, 0x44, 0x51, 0x45, 0x42, 0x43,
	0x77, 0x55, 0x41, 0x41, 0x34, 0x49, 0x42, 0x41, 0x51, 0x42, 0x2b, 0x4a, 0x68, 0x64, 0x4c, 0x41, 0x54, 0x53, 0x69,
	0x2f, 0x47, 0x42, 0x79, 0x32, 0x32, 0x34, 0x67, 0x76, 0x6d, 0x45, 0x6e, 0x77, 0x50, 0x65, 0x59, 0x4a, 0x50, 0x66,
	0x6d, 0x31, 0x5a, 0x4b, 0x34, 0x61, 0x64, 0x37, 0x6c, 0x0a, 0x59, 0x38, 0x37, 0x62, 0x78, 0x72, 0x59, 0x57, 0x55,
	0x36, 0x73, 0x4d, 0x50, 0x52, 0x6d, 0x6b, 0x72, 0x4e, 0x36, 0x38, 0x66, 0x42, 0x62, 0x47, 0x54, 0x38, 0x30, 0x68,
	0x74, 0x57, 0x37, 0x4c, 0x46, 0x6e, 0x72, 0x71, 0x62, 0x6e, 0x78, 0x7a, 0x41, 0x6d, 0x6c, 0x6d, 0x58, 0x4c, 0x45,
	0x2b, 0x31, 0x6c, 0x43, 0x79, 0x74, 0x37, 0x59, 0x7a, 0x2f, 0x53, 0x70, 0x50, 0x51, 0x75, 0x46, 0x48, 0x0a, 0x73,
	0x4e, 0x49, 0x6c, 0x62, 0x32, 0x59, 0x74, 0x47, 0x70, 0x69, 0x64, 0x56, 0x55, 0x7a, 0x35, 0x50, 0x67, 0x78, 0x35,
	0x6f, 0x62, 0x46, 0x65, 0x7a, 0x53, 0x39, 0x6e, 0x56, 0x79, 0x6f, 0x4c, 0x38, 0x32, 0x62, 0x78, 0x42, 0x65, 0x47,
	0x6e, 0x66, 0x72, 0x71, 0x4b, 0x4d, 0x4c, 0x2f, 0x34, 0x4e, 0x4e, 0x79, 0x78, 0x64, 0x64, 0x71, 0x57, 0x44, 0x61,
	0x34, 0x58, 0x32, 0x6b, 0x44, 0x63, 0x0a, 0x42, 0x70, 0x79, 0x30, 0x7a, 0x31, 0x47, 0x6a, 0x43, 0x34, 0x64, 0x77,
	0x38, 0x73, 0x65, 0x64, 0x44, 0x37, 0x35, 0x62, 0x33, 0x33, 0x57, 0x54, 0x68, 0x4e, 0x44, 0x67, 0x74, 0x4b, 0x6d,
	0x30, 0x52, 0x62, 0x55, 0x38, 0x62, 0x66, 0x68, 0x45, 0x68, 0x42, 0x4a, 0x73, 0x68, 0x46, 0x2f, 0x36, 0x53, 0x76,
	0x58, 0x45, 0x32, 0x45, 0x76, 0x4c, 0x71, 0x69, 0x67, 0x58, 0x53, 0x2f, 0x68, 0x56, 0x0a, 0x52, 0x78, 0x6f, 0x6d,
	0x6e, 0x46, 0x54, 0x75, 0x2b, 0x63, 0x73, 0x70, 0x43, 0x57, 0x37, 0x53, 0x42, 0x46, 0x31, 0x6d, 0x64, 0x50, 0x70,
	0x33, 0x51, 0x65, 0x62, 0x4c, 0x6a, 0x35, 0x6f, 0x61, 0x41, 0x36, 0x71, 0x59, 0x53, 0x6a, 0x5a, 0x47, 0x49, 0x63,
	0x32, 0x42, 0x41, 0x61, 0x59, 0x58, 0x6c, 0x6b, 0x4e, 0x34, 0x6d, 0x65, 0x6d, 0x69, 0x50, 0x76, 0x33, 0x6f, 0x4b,
	0x6f, 0x52, 0x6b, 0x0a, 0x54, 0x7a, 0x65, 0x56, 0x6a, 0x50, 0x41, 0x71, 0x4f, 0x74, 0x30, 0x6e, 0x63, 0x43, 0x58,
	0x2f, 0x6a, 0x4f, 0x71, 0x43, 0x64, 0x6e, 0x73, 0x56, 0x44, 0x6a, 0x69, 0x48, 0x4a, 0x47, 0x54, 0x4c, 0x73, 0x30,
	0x74, 0x7a, 0x62, 0x35, 0x54, 0x6f, 0x33, 0x69, 0x44, 0x6a, 0x42, 0x7a, 0x51, 0x36, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d,
	0x2d, 0x45, 0x4e, 0x44, 0x20, 0x43, 0x45, 0x52, 0x54, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x45, 0x2d, 0x2d, 0x2d,
	0x2d, 0x2d, 0x0a, 0x00};

struct pthread_arg {
	int argc;
	char** argv;
};

int greeter_client_cb(void* args)
{
	// Instantiate the client. It requires a channel, out of which the actual RPCs
	// are created. This channel models a connection to an endpoint (in this case,
	// localhost at port 50051). We indicate that the channel isn't authenticated
	// (use of InsecureChannelCredentials()).

	int argc;
	char** argv;
	int num = 0;
	bool secure_mode = false;
	char* p;

	char* addr;

	argc = ((struct pthread_arg*)args)->argc;
	argv = ((struct pthread_arg*)args)->argv;

	if (argc > 1) {
		for (int i = 1; i < argc; i++) {
			p = argv[i];
			if (strcmp(p, "-s") == 0) {
				secure_mode = true;
				printf("SECURE ON!\n");
			} else {
				addr = p;
				printf("ADDR : %s\n", addr);
			}
		}
	} else {
		printf("Usage: \tgrpc_client <option> [IP:PORT]\n");
		printf("\t<option>\t-s : secure mode\n");
		printf("\t<example>\tgrpc_client -s 192.168.1.2:50051\n");
		return 0;
	}

	std::shared_ptr<Channel> myChannel;

	if (secure_mode) {
		std::shared_ptr<ChannelCredentials> creds;
		grpc::SslCredentialsOptions ssl_opts;
		ssl_opts.pem_root_certs = test_root_cert;
		grpc::ChannelArguments channel_args = grpc::ChannelArguments();
		channel_args.SetSslTargetNameOverride("foo.test.google.fr");
		creds = grpc::SslCredentials(grpc::SslCredentialsOptions(ssl_opts));

		myChannel = grpc::CreateCustomChannel(addr, creds, channel_args);
	} else {
		myChannel = grpc::CreateChannel(addr, grpc::InsecureChannelCredentials());
	}
	GreeterClient greeter(myChannel);

	std::string user("world");
	std::cout << "trying say hello" << std::endl;
	std::string reply = greeter.SayHello(user);
	std::cout << "Greeter received: " << reply << std::endl;
	/*
	std::cout << "trying say hello again" << std::endl;
	reply = greeter.SayHelloAgain(user);
	std::cout << "Greeter received: " << reply << std::endl;
	*/

	return 0;
}

/****************************************************************************
 * Name: addressbook_main
 ****************************************************************************/

/*
 * Definition for handling pthread
 */
#define GREETER_CLIENT_PRIORITY 100
#define GREETER_CLIENT_STACK_SIZE 16384
#define GREETER_CLIENT_SCHED_POLICY SCHED_RR

extern "C" {
int greeter_client_main(int argc, char** argv)
{
	struct pthread_arg args;
	args.argc = argc;
	args.argv = argv;

	setenv("GRPC_VERBOSITY", "DEBUG", 1);
	pthread_t tid;
	pthread_attr_t attr;
	struct sched_param sparam;

	int r;

	/* 1. set a priority */
	sparam.sched_priority = GREETER_CLIENT_PRIORITY;
	if ((r = pthread_attr_setschedparam(&attr, &sparam)) != 0) {
		printf("%s: pthread_attr_setschedparam failed, status=%d\n", __func__, r);
		return -1;
	}

	if ((r = pthread_attr_setschedpolicy(&attr, GREETER_CLIENT_SCHED_POLICY)) != 0) {
		printf("%s: pthread_attr_setschedpolicy failed, status=%d\n", __func__, r);
		return -1;
	}

	/* 2. set a stacksize */
	if ((r = pthread_attr_setstacksize(&attr, GREETER_CLIENT_STACK_SIZE)) != 0) {
		printf("%s: pthread_attr_setstacksize failed, status=%d\n", __func__, r);
		return -1;
	}

	/* 3. create pthread with entry function */
	if ((r = pthread_create(&tid, &attr, (pthread_startroutine_t)greeter_client_cb, (void*)&args)) != 0) {
		printf("%s: pthread_create failed, status=%d\n", __func__, r);
		return -1;
	}

	/* Wait for the threads to stop */
	pthread_join(tid, NULL);

	return 0;
}
}
